<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/8/5
 * Time: 17:21
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Bankcardtype extends Base
{
    /**
     * 银行首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where['bt_status'] = 1;
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = db('bankcard_type')->where($where)->count();// 获取总条数
            $lists = db('bankcard_type')
                ->field('bt_id, bt_name, bt_logo, bt_time, bt_card_type')
                ->where($where)
                ->order('bt_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加银行
     * @return mixed|\think\response\Json
     */
    public function addBt(){
        if(Request::instance()->isPost()){
            $bt_name = trim(input('bt_name'));
            $bt_logo = input('bt_logo');
            $bt_card_type = trim(input('bt_card_type'));
            $insert = [
                'bt_name' => $bt_name,
                'bt_logo' => $bt_logo,
                'bt_card_type' => $bt_card_type,
                'bt_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_bankcard_type')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }
    
    /**
     * 编辑银行
     * @return mixed|\think\response\Json
     */
    public function editBt(){
        $bt_id = input('bt_id');
        if(Request::instance()->isPost()){
            $bt_name = trim(input('bt_name'));
            $bt_logo = input('bt_logo');
            $bt_card_type = trim(input('bt_card_type'));
            $update = [
                'bt_id' => $bt_id,
                'bt_name' => $bt_name,
                'bt_logo' => $bt_logo,
                'bt_card_type' => $bt_card_type,
                'bt_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_bankcard_type')->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $bank = Db::table('up_bankcard_type')->where('bt_id', $bt_id)->field('bt_name, bt_logo, bt_card_type')->find();
        $this->assign([
            'bank' => $bank,
            'bt_id' => $bt_id
        ]);
        return $this->fetch();
    }
    
    /**
     * 删除银行
     * @return \think\response\Json
     */
    public function delBt(){
        $bt_id = input('id');
        $res = Db::table('up_bankcard_type')->where('bt_id', $bt_id)->setField('bt_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}